package com.financing.wap.service.impl;


import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.financing.wap.common.ResultMap;
import com.financing.wap.common.BizEnums.CommonEnums;
import com.financing.wap.dao.ISalesPrizesDao;
import com.financing.wap.dao.ISalesPrizesLogDao;
import com.financing.wap.dto.SalesPrizesDTO;
import com.financing.wap.dto.SalesPrizesLogDTO;
import com.financing.wap.dto.SalesPrizesPeopleDTO;
import com.financing.wap.service.ISalesPrizesPeopleService;
import com.financing.wap.service.ISalesPrizesService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.DateUtil;
/**
 * sales_prizes  
 * 奖项配置表 业务访问层实现类
 * @author huqiyun
 *
 */
 
@Repository("salesPrizesService")
public class SalesPrizesServiceImpl implements ISalesPrizesService{

	@Autowired
	private ISalesPrizesDao salesPrizesDao;
	

	@Autowired
	private ISalesPrizesPeopleService salesPrizesPeopleService;
	
	@Autowired
	private ISalesPrizesLogDao salesPrizesLogDao;

	@Override
	public int insert(SalesPrizesDTO salesPrizes) throws SQLException {
		DTOdateSetUtil.setCreateInfo(salesPrizes);
		DTOdateSetUtil.setUpdateInfo(salesPrizes);
		salesPrizes.setDeleteTag(CommonEnums.DELETE_FLAG_1);
		return salesPrizesDao.insert(salesPrizes);
	}

	@Override
	public void update(SalesPrizesDTO salesPrizes) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(salesPrizes);
		salesPrizesDao.update(salesPrizes);
	}

	@Override
	public void updateForce(SalesPrizesDTO salesPrizes) throws SQLException {
		DTOdateSetUtil.setUpdateInfo(salesPrizes);
		salesPrizesDao.updateForce(salesPrizes);
	}

	@Override
	public void delete(Integer id) throws SQLException {
		salesPrizesDao.delete(id);
	}
	
	@Override
	public List<SalesPrizesDTO> queryList(SalesPrizesDTO salesPrizes) throws SQLException {
		return salesPrizesDao.queryList(salesPrizes);
	}

	@Override
	public int queryListCount(SalesPrizesDTO salesPrizes) throws SQLException {
		return salesPrizesDao.queryListCount(salesPrizes);
	}

	@Override
	public SalesPrizesDTO loadById(Integer id) throws SQLException {
		return salesPrizesDao.loadById(id);
	}
 	@Override
	public SalesPrizesDTO queryListGetFirst(SalesPrizesDTO salesPrizes)
			throws SQLException {
		 List<SalesPrizesDTO> queryList = queryList(salesPrizes);
		 if(queryList==null||queryList.size()==0){
			 return new SalesPrizesDTO();
		 }
		 return queryList.get(0);
	}

	@Override
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public synchronized void salesPrizes(String loginName,ResultMap result) throws SQLException {
		SalesPrizesPeopleDTO salesPrizesPeople=new SalesPrizesPeopleDTO();
		salesPrizesPeople.setSalesPhone(loginName);
		salesPrizesPeople.setDeleteTag(CommonEnums.DELETE_FLAG_1);
		SalesPrizesPeopleDTO people = salesPrizesPeopleService.queryListGetFirst(salesPrizesPeople);
		if(people==null||people.getId()==null){
			//不在名单内 直接返回
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_4108);
			return ;
		}
		Integer prizesCount = people.getPrizesCount()==null?0:people.getPrizesCount();
		Integer prizesPassCount = people.getPrizesPassCount()==null?0:people.getPrizesPassCount();
		if(prizesCount<=prizesPassCount||prizesCount==0){
			//抽奖机会为0
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_4109);
			return ;
		}
		
		//查询出所有未抽出的奖项
		SalesPrizesDTO salesPrizes=new SalesPrizesDTO();
		salesPrizes.setDeleteTag(CommonEnums.DELETE_FLAG_1);
		salesPrizes.setPrizesPassCount(1);
		salesPrizes.setBegin(0);
		salesPrizes.setPageSize(1);
		List<SalesPrizesDTO> sale = salesPrizesDao.queryListSend(salesPrizes);
		if(sale.size()==0){
			//没有奖项了
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_4110);
			return ;
		}
		
		
		//用户中奖   修改奖品
		SalesPrizesDTO salesPrizesDTO = sale.get(0);
		//更新已抽奖 抽奖时间更新
		salesPrizesDTO.setPrizesPassCount(1);
		DTOdateSetUtil.setUpdateInfo(salesPrizesDTO);
		salesPrizesDao.update(salesPrizesDTO);
		
		//用户抽奖次数+1
		people.setPrizesPassCount(prizesPassCount==null?1:(prizesPassCount+1));
		DTOdateSetUtil.setUpdateInfo(people);
		salesPrizesPeopleService.update(people);
		
		//流水记录
		SalesPrizesLogDTO salesPrizesLog=new SalesPrizesLogDTO();
		DTOdateSetUtil.setCreateInfo(salesPrizesDTO);
		DTOdateSetUtil.setUpdateInfo(salesPrizesLog);
		salesPrizesLog.setDeleteTag(CommonEnums.DELETE_FLAG_1);
		salesPrizesLog.setPrizesDate(DateUtil.getDate());
		salesPrizesLog.setPrizesPeoplePhone(loginName);
		salesPrizesLog.setPrizesPeopleName(people.getSalesName());
		salesPrizesLog.setPrizesId(salesPrizesDTO.getId());
		salesPrizesLogDao.insert(salesPrizesLog);
		
		//返回中奖信息
		result.setAttribute("dto", salesPrizesDTO);
		result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		result.setStatus(ResultMap.STATUS.SUCCESS);
	}

	@Override
	public List<Map<String, String>> salesPrizesPeopleList()
			throws SQLException {
		return salesPrizesDao.salesPrizesPeopleList();
	}
}
